import sys
from flask import Flask
from flask import request
from flask import jsonify
from googletrans import Translator
translator = Translator()

app = Flask(__name__)
# http://flask.readthedocs.io/en/latest/config/#JSON_AS_ASCII
app.config['JSON_AS_ASCII'] = False

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/trans/<dest>', methods=['POST'])
def trans(dest):
    try:
        result = translator.translate(request.get_json(), dest=dest)
        if len(result) < 1:
            return "result len < 1"
        if hasattr(result[0], 'text'):
            return "result[0].text null " + print(result) + print(result.text) + len(result)
        return jsonify([o.text for o in result]) # https://stackoverflow.com/a/677685/346701
    except ValueError as err:
        return "ValueError: {0}".format(err)
    except Exception as err:
        return "Unexpected error: {0}".format(err)